package com.jss.lanouhn.fristapp.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.jss.lanouhn.fristapp.R;
import com.ybao.pullrefreshview.layout.BaseHeaderView;
import com.ybao.pullrefreshview.support.type.LayoutType;

/**
 * Created by Ybao on 2015/11/3 0003.
 */
public class NormalHeaderView extends BaseHeaderView {
   ImageView heart;
   ImageView box;


    public NormalHeaderView(Context context) {
        this(context, null);
    }

    public NormalHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NormalHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.refresh_header, this, true);
//        textView = (TextView) findViewById(R.id.text);
        heart = (ImageView) findViewById(R.id.iv_refresh_heart);
//        progress = findViewById(R.id.progress);
        box = (ImageView) findViewById(R.id.iv_refresh_box);
    }

    @Override
    protected void onStateChange(int state) {
        if (heart == null || box == null ) {
            return;
        }
        heart.setVisibility(INVISIBLE);
        box.setVisibility(View.VISIBLE);
        switch (state) {
            case NONE:
                break;
            case PULLING:
                break;
            case LOOSENT_O_REFRESH:
                heart.setVisibility(VISIBLE);

                break;
            case REFRESHING:
                Animation animation= AnimationUtils.loadAnimation(getContext(),R.anim.set_animate_hearte);
                heart.startAnimation(animation);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        heart.setVisibility(INVISIBLE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                box.setImageResource(R.drawable.refresh_animate);
                AnimationDrawable animationDrawable = (AnimationDrawable) box.getDrawable();
                animationDrawable.start();


                break;
            case REFRESH_CLONE:
                heart.setVisibility(INVISIBLE);
                box.setVisibility(INVISIBLE);
                break;
        }

    }

    @Override
    public float getSpanHeight() {
        return getHeight();
    }


    @Override
    public int getLayoutType() {
        return LayoutType.LAYOUT_NORMAL;
    }
}
